import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_small_project/pages/home/components/home_list.dart';
import 'package:flutter_small_project/utils/tokenManager.dart';
import 'package:flutter_small_project/utils/toast.dart';
import 'components/home_nav.dart';


class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<HomePage> {
   //创建一个变量 timer ,用于接收 定时器
    Timer? _timer;
    int _timeCount = 60;
  // 头部的结构
  List<Widget> getTitleList() {
    return [
      Image.asset(
        'assets/images/notice@2x.png',
        width: 25,
      ),
      const Text(
        '社区',
        style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
      ),
      const Text(
        '公告',
        style: TextStyle(
            fontSize: 16, fontWeight: FontWeight.bold, color: Colors.orange),
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
   

    return Scaffold(
        backgroundColor: const Color.fromRGBO(229, 231, 241, 1),
        appBar: AppBar(
          title: const Text('享+社区'),
          backgroundColor: Colors.transparent,
          centerTitle: true, // 开区居中
        ),
        body: ListView(padding: const EdgeInsets.all(10), children: [
          // 测试弹窗的按钮
          // ElevatedButton(onPressed:(){
          // 测试弹窗
          // showAboutDialog(context: context,
          //   children: [
          //     const Text('你好')
          //   ]
          // );
          //   showDatePicker(context: context, firstDate:DateTime.now(), lastDate: DateTime.now().add(const Duration(days: 30)));

          // }, child: const Text('测试')),
          // 写三个按钮
          Row(
            children: [
              Expanded(
                  child: ElevatedButton(
                      onPressed: () {
                        tokenManager.setToken('123456');
                        //提示用户
                        PromptAction.showSuccess('存储成功');
                      },
                      child: const Text('存储Token'))),
              Expanded(
                  child: ElevatedButton(
                onPressed: () async {
                  final res = await tokenManager.getToken();
                  PromptAction.showSuccess(res);
                },
                child: const Text('获取Token成功'),
              )),
              Expanded(
                child: ElevatedButton(
                  onPressed: () async {
                    await tokenManager.deleteToken();
                  },
                  child: const Text('删除Token'),
                ),
              )
            ],
          ),
          const SizedBox(height: 10),
          Text(
            '还剩$_timeCount秒',
            textAlign: TextAlign.center,
          ),
          const SizedBox(height: 10),
          Row(children: [
            Expanded(
              child: ElevatedButton(
                onPressed: () {
                  _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                    setState(() {
                      _timeCount--;
                    });
                    print(_timeCount);
                  });
                },
                child: const Text('开启定时器'),
              ),
            ),
            Expanded(
              child: ElevatedButton(
                onPressed: () {
                  _timer?.cancel();
                },
                child: const Text('关闭定时器'),
              ),
            ),
            Expanded(
              child: ElevatedButton(
                onPressed: () {
                  Future.delayed(const Duration(seconds: 3), () {
                    PromptAction.showInfo('延时器执行了');
                  });
                },
                child: const Text('延时器'),
              ),
            )
          ]),

          HomeNav(),
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Image.asset('assets/images/banner@2x.jpg'),
          ),
          const HomeList(),
        ]));
  }
}
